package drds.data_propagate.binlog_event.protogenesis.event.binlog_management;

import drds.data_propagate.binlog_event.protogenesis.BinLogEvent;
import drds.data_propagate.binlog_event.protogenesis.BinlogEventType;
import drds.data_propagate.binlog_event.protogenesis.Buffer;
import drds.data_propagate.binlog_event.protogenesis.event.Header;
import lombok.Getter;
import lombok.Setter;

public final class RotateEvent extends BinLogEvent {

    public static final int rotate_event_offset = 0;

    public static final int maxFileNameLength = 512;
    @Setter
    @Getter
    private final String fileName;
    @Setter
    @Getter
    private final long position;

    public RotateEvent(Header header, Buffer buffer, FormatDescriptionEvent formatDescriptionEvent) {
        super(header);

        final int commonHeaderLength = formatDescriptionEvent.commonHeaderLength;
        final int eventPostHeaderLength = formatDescriptionEvent.eventPostHeaderLength[BinlogEventType.binlog_event_rotate_event - 1];
        //
        buffer.newReadedIndex(commonHeaderLength + rotate_event_offset);
        position = (eventPostHeaderLength != 0) ? buffer.getNextLittleEndian64SignedLong() : 4; // !uint8korr(buf +R_POS_OFFSET)

        final int filenameOffset = commonHeaderLength + eventPostHeaderLength;
        //读取除头信息之外的所有的数据
        int fileNameLength = buffer.limit() - filenameOffset;
        if (fileNameLength > maxFileNameLength - 1) {
            fileNameLength = maxFileNameLength - 1;
        }
        buffer.newReadedIndex(filenameOffset);
        fileName = buffer.getFixLengthStringWithNullTerminateCheck(fileNameLength);
    }


}
